/*
 * @lc app=leetcode.cn id=415 lang=typescript
 *
 * [415] 字符串相加
 */

// @lc code=start
function addStrings(num1: string, num2: string): string {
    const size = num1.length - num2.length;
    let i = 0;
    let zero = ''
    while (i < Math.abs(size)) {
        zero += '0';
        i++;
    }
    // console.log(zero)
    if (size > 0) {
        num2 = zero + num2;
    } else {
        num1 = zero + num1;
    }
    let r = '';
    let flag = false;
    for (let i = num1.length - 1; i >= 0; i--) {
        let n = Number(num1[i]) + Number(num2[i]);
        if (flag) {
            n = n + 1;
            flag = false;
        }
        let rr = n;
        if (n > 9) {
            rr = n % 10;
            flag = true;
        }
        r = rr + r;
    }
    if (flag) {
        r = '1' + r;
    }

    // console.log(r);
    return r;
};

// addStrings('401716832807512840963', '167141802233061013023557397451289113296441069')
// @lc code=end

